package com.jinglvhuanjing.app.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.jinglvhuanjing.app.R;
import com.jinglvhuanjing.app.bean.SheetBottomBean;
import com.jinglvhuanjing.app.dialog.SheetBottomListDialog;
import com.jinglvhuanjing.app.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.HashMap;

public class MySpinnerView extends FrameLayout {

    private static final String TAG = "common";

    public MySpinnerView(@NonNull Context context) {
        super(context);
        this.init(context);
    }

    public MySpinnerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    public MySpinnerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context);
    }

    private String mLabel;
    private String mValue;
    private String mKey;
    private ArrayList<SheetBottomBean> mItems;
    private void init(Context context){
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ScreenUtils.dip2px(context , 80) , ScreenUtils.dip2px(context , 25));
        setLayoutParams(params);
        LayoutInflater.from(context).inflate(R.layout.view_spinner_layout , this , true);
        setOnClickListener(this::clickEvent);
    }

    public void setParamTag(String label , String key){
        Log.d(TAG , String.format("设置下拉框参数标识：LABEL = %s , KEY = %s " , label , key));
        this.mLabel = label;
        this.mKey = key;
    }

    public void setData(ArrayList<SheetBottomBean> items){
        this.mItems = items;
    }

    private void clickEvent(View view){
        AppCompatTextView tv_spinner = findViewById(R.id.tv_spinner);
        SheetBottomListDialog dialog = new SheetBottomListDialog(view.getContext());
        if(this.mItems!=null){
            dialog.setData(mItems);
        }
        dialog.setSheetClickEvent(sheetBottomBean -> {
            this.mLabel = sheetBottomBean.getLabel();
            tv_spinner.setText(sheetBottomBean.getLabel());
            this.mValue = sheetBottomBean.getByteStr();
            dialog.dismiss();
        });
        dialog.show();
    }

    public HashMap<String , String> getValueMap(){
        HashMap<String , String> valueParam = new HashMap<>();
        if(TextUtils.isEmpty(this.mKey)){
            Log.d(TAG , String.format("获取下拉框值：LABEL = %s , KEY = %s , VALUE = %s" , this.mLabel , this.mKey , "NULL"));
            return valueParam;
        }
        String value =TextUtils.isEmpty(mValue) ? "" : mValue;
        valueParam.put(this.mKey ,  value);
        Log.d(TAG , String.format("获取下拉框值：LABEL = %s , KEY = %s , VALUE = %s" , this.mLabel , this.mKey , value));
        return valueParam;
    }

    public String getLabel(){
        return mLabel;
    }

    public String getValue(){
        return mValue;
    }

}
